package com.shu.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author ssm
 * @version V1.0
 * @description:
 * @date 2020/8/27 9:44
 */
public class SyncronizedLock {

    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();
        Condition condition1 = lock.newCondition();
        Condition condition2 = lock.newCondition();
        Condition condition3 = lock.newCondition();

        char[] a1 = "123456".toCharArray();
        char[] b1 = "abcdef".toCharArray();
        char[] c1 = "ABCDEF".toCharArray();

        new Thread(() -> {
            lock.lock();
            System.out.println("--A--");
            try {
                for (final char c : a1) {
                    System.out.println(c);
                    condition2.signal();
                    condition1.await();
                    System.out.println("--A1--");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }, "t1").start();


        new Thread(() -> {
            lock.lock();
            System.out.println("--B--");
            try {
                for (final char c : b1) {
                    System.out.println(c);
                    condition3.signal();
                    condition2.await();
                    System.out.println("--B1--");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }, "t2").start();


        new Thread(() -> {
            lock.lock();
            System.out.println("--C--");
            try {
                for (final char c : c1) {
                    System.out.println(c);
                    condition1.signal();
                    condition3.await();
                    System.out.println("--C1--");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }, "t3").start();

    }


}


